package com.xyht.sca_s.student_manage_system.modules.trainingSchool.controller;


import com.xyht.sca_s.student_manage_system.auth.util.JwtTokenUtil;
import com.xyht.sca_s.student_manage_system.common.annotation.RequestPass;
import com.xyht.sca_s.student_manage_system.common.commonReturn.CommonCodeEnum;
import com.xyht.sca_s.student_manage_system.common.commonReturn.CommonResult;
import com.xyht.sca_s.student_manage_system.common.commonReturn.ResponseResult;
import com.xyht.sca_s.student_manage_system.common.desEncodeAndDecode.annotation.SecretBody;
import com.xyht.sca_s.student_manage_system.modules.trainingSchool.entity.req.JobBatchAddReq;
import com.xyht.sca_s.student_manage_system.modules.trainingSchool.entity.req.SmsTsJobReq;
import com.xyht.sca_s.student_manage_system.modules.trainingSchool.entity.req.SmsTsProfessionReq;
import com.xyht.sca_s.student_manage_system.modules.trainingSchool.service.SmsTsJobService;
import com.xyht.sca_s.student_manage_system.modules.welcomeNewStudent.entity.req.FeesRecordBatchAddReq;
import com.xyht.sca_s.student_manage_system.modules.welcomeNewStudent.entity.req.SmsWnsIdListReq;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.concurrent.Future;

/**
 * <p>
 * 工种表 前端控制器
 * </p>
 *
 * @author XYHT
 * @since 2024-05-15
 */
@RestController
@SecretBody
@RequestMapping("/trainingSchool/job")
public class SmsTsJobController {

    @Resource
    private SmsTsJobService smsTsJobService;
    @Resource
    private JwtTokenUtil jwtTokenUtil;

    @ApiOperation(value = "添加工种")
    @PostMapping("/add")
    public ResponseResult add(@RequestBody SmsTsJobReq req) {
        return smsTsJobService.add(req);
    }

    @ApiOperation(value = "删除工种")
    @PostMapping("/delete")
    public ResponseResult delete(@RequestBody SmsWnsIdListReq req) {
        smsTsJobService.removeByIds(req.getIdList());
        return CommonResult.success();
    }

    @ApiOperation(value = "修改工种")
    @PostMapping("/update")
    public ResponseResult update(@RequestBody SmsTsJobReq req) {
        return smsTsJobService.update(req);
    }

    @RequestPass
    @ApiOperation(value = "工种列表")
    @GetMapping("/list")
    public ResponseResult list(@RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
                               @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize,
                               @RequestParam(value = "professionId", required = false) String professionId,
                               @RequestParam(value = "ss", required = false) String ss) {
        return smsTsJobService.list(pageNum, pageSize, professionId, ss);
    }

    @ApiOperation(value = "工种批量导入")
    @PostMapping("/batchAddJob")
    public ResponseResult batchAddJob(@RequestBody JobBatchAddReq req, HttpServletRequest request) {
        String userId = jwtTokenUtil.getUserIdByRequest(request);
        ResponseResult commonCodeEnum;
        try {
            Future<ResponseResult> future = smsTsJobService.batchAddJob(req, userId);
            while (true) {
                if (future.isDone()) {
                    commonCodeEnum = future.get();
                    break;
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
            return CommonResult.failed(CommonCodeEnum.FAIL);
        }
        return commonCodeEnum;
    }

    @RequestPass
    @ApiOperation(value = "职业工种树")
    @GetMapping("/tree")
    public ResponseResult tree(@RequestParam(value = "professionId", required = false) String professionId,
                               @RequestParam(value = "ss", required = false) String ss) {
        return smsTsJobService.tree(professionId, ss);
    }

}

